/*
ID: pallab81
PROG:
LANG: C++
*/

//"I have not failed, I have just found 10000 ways that won't work.

#include <iostream>
#include <algorithm>
#include <vector>
#include <sstream>
#include <fstream>
#include <string>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>
#include <iomanip>

#include <ctime>
#include <cassert>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <climits>
#include <cstdlib>

using namespace std;

#define VI vector< int >
#define CI( x ) scanf("%d",&x)
#define CL( x ) scanf("%lld",&x)
#define CD( x ) scanf("%lf",&x )
#define foR(i1,st,ed) for(int i1 = st,j1 = ed ; i1 < j1 ; ++i1 )
#define fo(i,ed) foR( i , 0 , ed )
#define foE(i,st,ed) foR( i , st , ed+1 )
#define foit(i, x) for (typeof((x).begin()) i = (x).begin(); i != (x).end(); i++)
#define bip( X ) sleep( X )
#define Int long long
#define pb push_back
#define SZ(X) (int)(X).size()
#define LN(X) (int)(X).length()
#define mk make_pair
#define f first
#define s second
#define SET( ARRAY , VALUE ) memset( ARRAY , VALUE , sizeof(ARRAY) )

class BaseConfusion {
public:
    long long sum(int M, int N, int B);
};
vector< int > vi;
int base;
inline void parse( int var ) {
    vi.clear();
    while ( var > 0 ) {
        vi.pb( var %base );
        var/=base;
    }
    return ;
}

inline Int power( int indx , int BASE) {
    Int res=1LL;
    for (int i=0;i<indx;++i) {
        res*=BASE;
    }
    return res;
}
pair<Int,Int> dp[66][2];
int vis[66][2];
inline pair<Int,Int> go( int indx, int smaller ) {
    if ( indx==-1 ) {
        return mk( 0,1 );
    }
    if ( vis[indx][smaller]!=-1 ) {
        return dp[ indx ][smaller];
    }
    vis[indx][smaller]=1;

    int to = smaller==0 ? vi[ indx ] : base-1 ;
    pair<Int,Int> RET = mk(0,0);

    for (int d=0;d<=to;++d) {
        int nxtsmaller = smaller|( d < vi[indx] ? 1 : 0 );
        pair<Int,Int> v = go( indx-1, nxtsmaller );
        RET.f+= ( v.f+ ( power(indx, base+1 )*d*v.s) ) ;
        RET.s+= v.s;
    }
    return dp[ indx][smaller] = RET;
}
inline Int doit( int N ) {
    if ( N==0 )return 0;
    SET(vis,-1);
    parse( N );
    return go( SZ(vi)-1, 0 ).f;
}
long long BaseConfusion :: sum(int M, int N, int B) {
    base = B;
    Int res = doit( N ) - doit( M-1 );
    return res;

}
// BEGIN KAWIGIEDIT TESTING
// Generated by KawigiEdit 2.1.8 (beta) modified by pivanof
#include <iostream>
#include <string>
#include <vector>
using namespace std;
bool KawigiEdit_RunTest(int testNum, int p0, int p1, int p2, bool hasAnswer, long long p3) {
    cout << "Test " << testNum << ": [" << p0 << "," << p1 << "," << p2;
    cout << "]" << endl;
    BaseConfusion *obj;
    long long answer;
    obj = new BaseConfusion();
    clock_t startTime = clock();
    answer = obj->sum(p0, p1, p2);
    clock_t endTime = clock();
    delete obj;
    bool res;
    res = true;
    cout << "Time: " << double(endTime - startTime) / CLOCKS_PER_SEC << " seconds" << endl;
    if (hasAnswer) {
        cout << "Desired answer:" << endl;
        cout << "\t" << p3 << endl;
    }
    cout << "Your answer:" << endl;
    cout << "\t" << answer << endl;
    if (hasAnswer) {
        res = answer == p3;
    }
    if (!res) {
        cout << "DOESN'T MATCH!!!!" << endl;
    } else if (double(endTime - startTime) / CLOCKS_PER_SEC >= 2) {
        cout << "FAIL the timeout" << endl;
        res = false;
    } else if (hasAnswer) {
        cout << "Match :-)" << endl;
    } else {
        cout << "OK, but is it right?" << endl;
    }
    cout << "" << endl;
    return res;
}
int main() {
    bool all_right;
    all_right = true;

    int p0;
    int p1;
    int p2;
    long long p3;

    {
        // ----- test 0 -----
        p0 = 14;
        p1 = 18;
        p2 = 3;
        p3 = 129ll;
        all_right = KawigiEdit_RunTest(0, p0, p1, p2, true, p3) && all_right;
        // ------------------
    }

    {
        // ----- test 1 -----
        p0 = 1;
        p1 = 10;
        p2 = 16;
        p3 = 55ll;
        all_right = KawigiEdit_RunTest(1, p0, p1, p2, true, p3) && all_right;
        // ------------------
    }

    {
        // ----- test 2 -----
        p0 = 100;
        p1 = 100;
        p2 = 10;
        p3 = 121ll;
        all_right = KawigiEdit_RunTest(2, p0, p1, p2, true, p3) && all_right;
        // ------------------
    }

    {
        // ----- test 3 -----
        p0 = 209881;
        p1 = 210565;
        p2 = 3;
        p3 = 3141592653ll;
        all_right = KawigiEdit_RunTest(3, p0, p1, p2, true, p3) && all_right;
        // ------------------
    }

    if (all_right) {
        cout << "You're a stud (at least on the example cases)!" << endl;
    } else {
        cout << "Some of the test cases had errors." << endl;
    }
    return 0;
}
// END KAWIGIEDIT TESTING





//Powered by KawigiEdit 2.1.8 (beta) modified by pivanof!
// kate: indent-mode cstyle; space-indent on; indent-width 0; 
